package com.chen.pfm.utils;


import com.chen.pfm.entity.ChartData;
import com.chen.pfm.entity.Classify;

import com.chen.pfm.entity.DisplayBill;
import com.chen.pfm.entity.ReportForms;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;


import java.util.*;

public class ClassifyDBUtil {
    static final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x2022001, "ClassifyDBUtil");
    static final String uriString = "dataability:///com.chen.pfm.ClassifyDataAbility/classify";

    public static List<Classify> queryAllClassifyByCtype1(Context context, int ctype1) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"cid", "uid", "cname", "ctype", "cctime"};
        Uri uri = Uri.parse(uriString);
        ResultSet resultSet = helper.query(uri, columns, predicates);
        List<Classify> lists = new ArrayList<>();
        if (resultSet.getRowCount() > 0) {
            // 处理结果
            resultSet.goToFirstRow();
            do {
                int ctype = resultSet.getInt(3);
                Classify classify = new Classify();
                classify.setCtype(ctype);
                if (classify.getCtype_1() == ctype1) {
                    String cid = resultSet.getString(0);
                    int uid = resultSet.getInt(1);
                    String cname = resultSet.getString(2);
                    String cctime = resultSet.getString(4);
                    classify.setCid(cid);
                    classify.setUid(uid);
                    classify.setCname(cname);
                    classify.setCctime(cctime);
                    // 在此处理ResultSet中的记录;
                    lists.add(classify);
                }
            } while (resultSet.goToNextRow());

        }
        Collections.sort(lists, new Comparator<Classify>() {
            @Override
            public int compare(Classify o1, Classify o2) {
                //这里使用时间字段排序 如果要用其他字段直接从bean实体中取
                return o1.getCctime().compareTo(o2.getCctime());
                //上面使用的是降序排序，如果正序 直接使用o1-o2即可
            }
        });
        return lists;
    }

    //得到一个分类
    public static int getUniqueType(Context context, int ctype_1, int ctype_3, int uid) {
        int ctype = ctype_1 * 10000 + getType2() + ctype_3;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("ctype", ctype);
        predicates.equalTo("uid", uid);
        String[] columns = new String[]{"cid"};
        Uri uri = Uri.parse(uriString);
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        if (resultSet.getRowCount() == 0) {
            return ctype;
        } else {
            return getUniqueType(context, ctype_1, ctype_3, uid);
        }
    }

    public static int getType2() {
        Random random = new Random();
        int n = random.nextInt(999) + 1;
        int ctype_2;
        if (n >= 100) {
            ctype_2 = n * 10;
        } else if (n >= 10) {
            ctype_2 = n * 100;
        } else {
            ctype_2 = n * 1000;
        }
        return ctype_2;
    }

    //过去七天饼状图数据
    public static List<ChartData> statisticalMoneyPastSevenDays(Context context, int type1) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"cid", "cname"};
        Uri uri = Uri.parse(uriString);
        ResultSet resultSet = helper.query(uri, columns, predicates);
        //得到过去七天的日期
        String[] pastSevenDate = DateUtils.getPastSevenDate();
        //统计过去七天的钱
        double allmoney;
        List<ChartData> list = new ArrayList<>();
        ChartData chartData = null;
        if (resultSet.getRowCount() > 0) {
            // 处理结果
            resultSet.goToFirstRow();
            do {
                //对每一个分类进行分组查找，统计过去七天的全部账单
                String cid = resultSet.getString(0);
                //初始化钱为0
                allmoney = 0;
                //得到cid后，对用户的全部订单进行，分类统计
                for (int i = 0; i < pastSevenDate.length; i++) {
                    allmoney += AccountDBUtil.statisticsMoneyBy_Cid_Artime_type1(context, cid, pastSevenDate[i], type1);
                }


                if (allmoney > 0) {//存在账单
                    //这里需要保证cid和ctype唯一的映射关系，否则应该以ctype为分类的主键
                    String cname = resultSet.getString(1);
                    chartData = new ChartData(cname, allmoney);

                    // 在此处理ResultSet中的记录;
                    list.add(chartData);
                } else {
                    //说明次此分类没有账单，不进行统计
                }

            } while (resultSet.goToNextRow());

        }
        return list;
    }

    //过去一个月饼状图数据和  过去一个月饼状图数据
    public static List<ChartData> statisticalMoneyCurrentMonthOrYear(Context context, String currentTime, int type1) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"cid", "cname"};
        Uri uri = Uri.parse(uriString);
        ResultSet resultSet = helper.query(uri, columns, predicates);
        List<ChartData> list = new ArrayList<>();
        ChartData chartData = null;
        if (resultSet.getRowCount() > 0) {
            // 处理结果
            resultSet.goToFirstRow();
            do {
                String cid = resultSet.getString(0);
                //得到cid后，对用户的全部订单进行，分类统计

                double allmoney = AccountDBUtil.statisticsMoneyBy_Cid_Artime_type1(context, cid, currentTime, type1);
                if (allmoney > 0) {//存在账单
                    //这里需要保证cid和ctype唯一的映射关系，否则应该以ctype为分类的主键
                    String cname = resultSet.getString(1);

                    chartData = new ChartData(cname, allmoney);

                    // 在此处理ResultSet中的记录;
                    list.add(chartData);
                } else {
                    //说明次此分类没有账单，不进行统计
                }

            } while (resultSet.goToNextRow());

        }
        return list;


    }


    public static Classify queryOneClassifyByCid(Context context, String findCid) throws DataAbilityRemoteException {
        Classify classify = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("cid", findCid);
        ;
        String[] columns = new String[]{"cid", "uid", "cname", "ctype"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet rs = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (rs.getRowCount() > 0) {
                //结果指针移到第一行
                rs.goToFirstRow();

                String cid = rs.getString(0);
                int uid = rs.getInt(1);
                String cname = rs.getString(2);
                int ctype = rs.getInt(3);
                classify = new Classify();
                classify.setCid(cid);
                classify.setUid(uid);
                classify.setCname(cname);
                classify.setCtype(ctype);
            } else {
                return null;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return classify;
    }

    public static List<ReportForms> statisticsReportByRtimeAndType1(Context context, String artime, int type1) throws DataAbilityRemoteException {
        List<ReportForms> reportForms = new ArrayList<>();
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"cname", "ctype", "cid"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet rs = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (rs.getRowCount() > 0) {
                //结果指针移到第一行
                rs.goToFirstRow();
                do {
                    String cname = rs.getString(0);
                    int ctype = rs.getInt(1);
                    String cid = rs.getString(2);
                    if (ctype / 10000 == type1) {
                        int i = AccountDBUtil.statisticsNumberBy_Cid_Artime(context, cid, artime);
                        if (i != 0) {
                            ReportForms reportForm = new ReportForms();
                            reportForm.setName(cname);
                            reportForm.setN(i);
                            reportForms.add(reportForm);
                        }
                    }
                } while (rs.goToNextRow());

            } else {
                return null;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        Collections.sort(reportForms);
        return reportForms;
    }

    public static boolean isExistByCid(Context context, String cid) {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("cid", cid);
        Uri uri = Uri.parse(uriString);
        String[] columns = new String[]{"cid"};
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet rs = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (rs.getRowCount() > 0) {
                flag = true;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean isExistByCnameAndCtype1(Context context, int ctype1, String cname, int uid) {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("cname", cname);
        predicates.equalTo("uid", uid);
        String[] columns = new String[]{"ctype"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet rs = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (rs.getRowCount() > 0) {
                rs.goToFirstRow();
                Classify classify = new Classify();
                classify.setCtype(rs.getInt(0));
                if (classify.getCtype_1() == ctype1) {
                    flag = true;
                } else {
                    flag = false;
                }
            } else {
                flag = false;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static int insertClassify(Context context, Classify classify) {
        int i = -1;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        ValuesBucket valuesBucket = classify.getValuesBucketfromObject();
        try {
            i = helper.insert(uri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return i;
    }



    public static int deleteClassifyByCid(Context context, String cid) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("cid", cid);
        int i = helper.delete(uri, predicates);
        return i;
    }

    public static int deleteByUid(Context context, Integer uid) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", uid);
        int i = helper.delete(uri, predicates);
        return i;
    }

}
